define(['jquery','api','user','pages/index/model','Vue','text!pages/order/index.html','util'],function($,api,user,model,Vue,tpl,util){
	var view,
	    vm,
	    timer,
	    localHq;
	var controller={
	   init:function () {
	       var str,
	           data;
	       view=$('#page');
	       if(!user.checkLogin())return;
	       api.initFooter(1);
	       util.setTitle('持仓单');	       
	       this.bind();
	       this.loadgoods();
	    },
	    bind:function(){       
	       if(vm){
	          vm.show=true;
	       }else{
	       	  $('#pages').append('<div id="orders"></div>');
	          vm=new Vue({
	          	  el:'#orders',
	              data:{
	                 fpl:'--',
	                 show:true,
	                 orders:false,
	                 closeOrder:false,
	                 showClose:false,
	                 showCloseAll:false
	              },
	              filters:{
	                 lowHigh:util.lowHigh
	              },
	              methods:{
	                 close:function(order,event){
		              	if (event) {
		              	   //event.preventDefault();
		              	   event.stopPropagation();
		              	}
		                this.closeOrder=order;
		                this.showClose=true;
		              },
		              cancelClose:function(){
		              	this.showClose=false;
		              },
		              doClose:function(){
			              	var order=this.closeOrder,
			              	    postData={
								    ordersn:order.opensn,
								    goodscode:order.goodscode,
								    orderamount:+order.openamount
							    };
			              	api.closeOrder(postData,function(){
			              	    util.msg('平仓成功！');
			              	});
			              	this.showClose=false;
		              },
		              showDetail:function(opensn,event){
		                if(event.target.tagName.toLowerCase()!='input')gotoUrl('orderDetail.html?refresh=1&opensn='+opensn)
		              },
		              showCloseAllPanel:function(){
		              	this.showCloseAll=true;
		              },
		              cancelCloseAll:function(){
		              	this.showCloseAll=false;
		              },
		              doCloseAll:function(){
		              	  var  orders=this.orders,
		              	       len=orders.length,
		              	       goodscode,
		              	       goodscodeArr=[];
//		              	  if(len!=0)for(var i=0;i<len;i++){
//					      	 goodscode=orders[i].goodscode;
//					         if(goodscodeArr.indexOf(goodscode)==-1){
//					         	goodscodeArr.push(goodscode);
//					         }
//					      }
//		              	  if(goodscodeArr.length)closeOrder({goodscode:goodscodeArr});
		              	  api.closeOrderAll(function(){
			              	    util.msg('平仓成功！')
                                            vm.orders = [];// 删除平仓成功的vm数据 20161209dyx
			              });
		              	  this.showCloseAll=false;
		              }
	              },
	              template:tpl
	          })
	       }
	    },
	    refreshOrder:function(){
	    	var that=this,
	    	    temp={},
	    	    o,
	    	    cb=function(){
	    	        timer=setTimeout(function(){that.refreshOrdersn();},util.cfg.refreshTime);
	    	    };
	        if(vm.orders==false){
		        api.order({},function(rs){		        
		       		if(rs.retcode==='0'){
		       		    if(rs.orders){
		       		      for(var i=0;i<rs.orders.length;i++){
		       		         o=rs.orders[i];
		       		         temp[o.opensn]=o;
		       		      }
		       		      vm.orders=temp; 
		       		    }else{
		       		      vm.orders={};
		       		    }
		       		    if(vm.show){
		       		    	cb(); 
		       		    }
		       		}
		        });
	        }else{
	            cb();
	        }
	    },
	    loadgoods:function(){
	    	var that=this,
	    	    cb=function(){
	    			that.refreshOrder();
	    	    };
	       if(model.code==null){
	   	   		api.goodsInfo(function(data){
	   	   		    if(!data||(data.errorcode&&data.errorcode!=0)||data.retcode!='0'){
	   	   		        return;
	   	   		    }
	   	   		    model.addGoods(data);
	   	   		    cb();
	   	   		});
	       }else{
	          cb();
	       }
	    },
	    refreshOrdersn:function(){
	    	var that=this,
	    	    cb=function(){that.refreshOrdersn();};
	       api.ordersn({},function(rs){
	       		if(rs.retcode==='0'){
	       		    that.changeOrders(rs.orders?rs.orders:[]); 
	       			localHq=rs.quotes;
	    	        that.refreshHq.call(that,rs.quotes);
	       		    if(vm.show){
	       		         timer=setTimeout(cb,util.cfg.refreshTime);
	       		    }
	       		}
	       },cb)	       	    	   
	    },
	    changeOrders:function(ordersnList){
	       var orders=vm.orders,
	           ordersn,
	           len=ordersnList.length;
	       for(var opensn in orders){
	           if(ordersnList.indexOf(opensn)==-1){
	              delete orders[opensn];
	           }
	       }
	       for(var i=0;i<len;i++){
	       	  ordersn=ordersnList[i];
	          if(!orders[ordersn]){
	              this.addOrder(ordersn);
	          }
	       }
	    },
	    addOrder:function(ordersn){
	       api.order(ordersn,function(rs){
	       		if(rs.retcode==='0'){
	       		   vm.orders[ordersn]=rs.orders[0];
	       		}else {
                    util.dealErrorCode(rs.retcode);// 20161209dyx:增加提示信息
                }
	       })	
	    },
	    refreshHq:function(hq){
	    	var orders=vm.orders,
	    	    fpl,
	    	    allFpl=0;
	    	for(var i in orders){
	    	    fpl=this.getFpl(orders[i],hq);
	    	    orders[i].clearpl=fpl;
	    	    allFpl+=fpl*100;
	    	} 
	    	vm.fpl=(allFpl/100).toFixed(2);
	    },
	    getFpl:function(order,hq){
	    	var goodscode=order.goodscode,
	    	    goods=model.goods_arr[goodscode],
	    	    mindecimal=goods.mindecimal,
	    	    groupid=goods.groupid,
	    	    close=hq[groupid].close,
	    	    pt,
	    	    newPrice,
	    	    fpl;
            pt = order.direction == 98 ? goods.sellpt : goods.buypt;
            newprice = +close +(+pt);
            fpl = (order.direction == 98 ? 1 : -1) * (newprice - order.holderprice) * order.openamount * goods.agreeunit / Math.pow(10, mindecimal);
            fpl = fpl.toFixed(2);
	    	return fpl;
	    },
        onRouteChange:function(){
	    	if(vm)vm.show=false;
	    	if(timer){
	    	   clearTimeout(timer);
	    	}
	    }
	}
    return controller;     
})